import { _decorator, Collider, Collider2D, Contact2DType, IPhysics2DContact, RigidBody2D, v3 } from 'cc';
import { GameWeapon } from './GameWeapon';
import { Weapon } from './Weapon';
import PlayerController from '../PlayerController';
import { Constant } from '../AshFramework/Datas/Constant';
import { EnemyController } from './EnemyController';
import { TLWLSJ_UIManager } from './UIManager';
import { AudioManager, Audios } from '../AshFramework/Managers/AudioManager';
const { ccclass } = _decorator;

enum Ani {
    None = "",
    Fire = "animation",
}

@ccclass('GameWeapon_QZB')
export class GameWeapon_QZB extends GameWeapon {

    rigidBody: RigidBody2D = null;
    collider: Collider2D = null;
    private _data: Weapon = null;
    private _harm: number = 10;
    private _armor: number = 999;

    protected start(): void {
        this.rigidBody = this.getComponent(RigidBody2D);
        this.collider = this.getComponent(Collider2D);
        this.collider.enabled = false;
        this.collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);

        this._data = this.Data;
        PlayerController.Instance.WeaponTs = this;
    }

    protected update(dt: number): void {
        this.node.setPosition(v3(0, 0, 0));
    }

    attack() {
        if (TLWLSJ_UIManager.Instance.IsLock) {
            this.getWeapon();
            return;
        }
        if (this.IsFire) return;

        if (this.IsStop) {
            this.IsStop = false;
            this.playAni(Ani.None, false);
            return;
        }

        this.IsFire = true;
        AudioManager.PlaySound(Audios.QZB);
        this.playAni(Ani.Fire, false, () => {
            this.IsFire = false;
            this.collider.enabled = true;
            this.scheduleOnce(() => { this.collider.enabled = false; })
            this.State = Ani.None;
            this.attack();
            //发起攻击
        });
    }

    onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
        // 只在两个碰撞体开始接触时被调用一次
        if (otherCollider.group == Constant.Group.ENEMY) {
            otherCollider.node.getComponent(EnemyController).hit(this._harm, this._armor)
        }
    }

}


